import requests
import json

from .WxPayTool import WxPayTool
from ..WxConfigParser import ConfigParser


class WxPayApi:
    # url = "https://api.mch.weixin.qq.com"

    def __init__(self):
        con = ConfigParser()
        self.wx = WxPayTool()
        self.url = con.get_pay_url()

    def pay_transactions(self, **kwargs):
        """
        支付接口
        :param kwargs:
        :return:
        """
        post_data = kwargs.get("payload")
        api_url = "/v3/pay/transactions/jsapi"
        url = self.url + api_url
        payload = json.dumps(post_data)
        headers, timestamp_str, nonce_str = self.wx.get_headers(data=payload, method="POST", url=api_url)
        response = requests.request("POST", url, headers=headers, data=payload)
        return (eval(response.text), timestamp_str, nonce_str)

    def refunds(self, **kwargs):
        """
        退款
        """
        post_data = kwargs.get("post_data")
        api_url = "/v3/refund/domestic/refunds"
        url = self.url + api_url
        payload = json.dumps(post_data)
        headers, timestamp_str, nonce_str = self.wx.get_headers(data=payload, method="POST", url=api_url)
        response = requests.request("POST", url, headers=headers, data=payload)
        if response.status_code != 200:
            raise ValueError(str(response.text))
        return (eval(response.text), timestamp_str, nonce_str)

    def transactions_select(self, out_trade_no):
        """
        查询订单
        """
        mchid = self.wx.mchid
        # post_data = kwargs.get("post_data")
        api_url = f"/v3/pay/transactions/out-trade-no/{out_trade_no}?mchid={mchid}"
        url = self.url + api_url
        payload = ""
        headers, timestamp_str, nonce_str = self.wx.get_headers(data=payload, method="GET", url=api_url)
        response = requests.request("GET", url, headers=headers, data=payload)
        return (eval(response.text), timestamp_str, nonce_str)

    def get_certificates(self):
        """
        获取平台证书
        """
        api_url = "/v3/certificates"
        url = self.url + api_url
        payload = ""
        headers, timestamp_str, nonce_str = self.wx.get_headers(data=payload, method="GET", url=api_url)
        response = requests.request("GET", url, headers=headers, data=payload)
        return eval(response.text)

    def transfer_batches(self, **kwargs):
        """
        发起商家转账
        """
        api_url = "/v3/transfer/batches"
        url = self.url + api_url
        print(url)
        payload = kwargs.get("payload")
        payload = json.dumps(payload)
        headers, timestamp_str, nonce_str = self.wx.get_headers(data=payload, method="POST", url=api_url)
        response = requests.request("POST", url, headers=headers, data=payload)
        return (eval(response.text), timestamp_str, nonce_str)

    def get_openid(self, code):
        """
        获取openid
        """
        info = {
            "appid": self.wx.appid,
            "secret": self.wx.secret,
            "js_code": code,
            "grant_type": "authorization_code"
        }
        response = requests.get(url='https://api.weixin.qq.com/sns/jscode2session', params=info)
        result = response.json()
        if "openid" in result.keys():
            openid = result['openid']
            return openid
        else:
            raise ValueError(result.get("errmsg"))